package com.eastfair.home.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.eastfair.core.base.R;
import com.eastfair.core.exception.BizException;
import com.eastfair.home.entity.BuilderRepairReport;
import com.eastfair.home.service.HomeRepairReportService;
import com.eastfair.projectcore.enumeration.MenuAffiliationEnum;
import com.eastfair.venuebooking.api.RepairReportFeign;
import com.eastfair.venueservice.consts.CommonConstants;
import com.eastfair.venueservice.dto.RepairReportDTO;
import com.eastfair.venueservice.entity.RepairReport;
import com.eastfair.venueservice.enumeration.RepairReportRepairTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 综合服务 报修申请 服务.
 *
 * @author dqq
 * @date 2022 /11/24
 */
@Slf4j
@Service
public class HomeRepairReportServiceImpl implements HomeRepairReportService {

    @Resource
    private RepairReportFeign repairReportFeign;

    @Override
    public RepairReport saveRepairReport(RepairReportDTO repairReportDTO) {
        log.info("saveRepairReport - 保存报修申请, repairReportDTO={}", repairReportDTO);
        R<RepairReport> result = repairReportFeign.saveRepairReport(repairReportDTO);
        log.info("saveRepairReport - 完成, result={}", result);
        if (!result.getIsSuccess()) {
            throw BizException.wrap(result.getCode(), result.getMsg());
        }
        return result.getData();
    }

    @Override
    public RepairReport saveRepairReportByBuilder(BuilderRepairReport builderRepairReport) {
        // 数据传递中要忽略的属性
        List<String> ignoreProperties = new ArrayList<>();
        Collections.addAll(ignoreProperties, CommonConstants.COMMON_IGNORE_PROPERTIES);
        Collections.addAll(ignoreProperties, "repairStatus", "checkResult", "repairOpinion", "isNeedCompensate", "completeRemark");
        String[] ignoreArray = new String[ignoreProperties.size()];
        ignoreProperties.toArray(ignoreArray);
        CopyOptions copyOptions = CopyOptions
                .create()
                .setIgnoreProperties(ignoreArray);
        RepairReportDTO repairReportDTO = BeanUtil.toBean(builderRepairReport, RepairReportDTO.class, copyOptions);
        repairReportDTO.setRepairType(RepairReportRepairTypeEnum.HOME_REPAIR);
        repairReportDTO.setBelongSystem(MenuAffiliationEnum.P_COMPOSITE_PRODUCER.getCode());
        return saveRepairReport(repairReportDTO);
    }
}